<?php

/**
 * Subclass for performing query and update operations on the 'ringtone' table.
 *
 * 
 *
 * @package lib.model
 */ 
class RingtonePeer extends BaseRingtonePeer
{
    public static function getRingtoneById($rtId)
    {
    	$con = new MysqlDAL('propel');
    	$sql = "SELECT total_set, total_download, imuzik_last_downloaded,
            imuzik_hidden_time, vocal_path FROM ringtone WHERE id = '{$rtId}'";
    	return $con->doSelect($sql);
    }
    
    public static function testTop(){
        $con = new MysqlDAL('propel');
        $sql = "SELECT * from ringtone where id = 100";
        return $con->doSelect($sql);
    }

    public static function removeTopHot($rtId){
        $con = new MysqlDAL('propel');
        $sql = "UPDATE ringtone
            SET top_order = null
            WHERE ringtone.id = '".$rtId."'";
        return $con->doUpdate($sql);
    }

    public static function updateTopHot($rtId){
        $con = new MysqlDAL('propel');
        $timestamp = date('Y-m-d H:i:s', time());
        $sql = "UPDATE ringtone
            SET top_order = '".$timestamp."'
            WHERE ringtone.id = '".$rtId."'";
        return $con->doUpdate($sql);
    }
    
    public static function getringtone_ims($rtId)
    {
    	$con = new MysqlDAL('propel');
    	$sql = "SELECT imuzik_hidden_time, total_set FROM ringtone WHERE id = '{$rtId}'";
    	return $con->doSelect($sql);
    }
 	// Function danh cho module remove nhac cho
   public static function getTotalRemove($filters = null)
    {
    	$con = new MysqlDAL('propel');
    	$sql = "SELECT COUNT(*) AS total FROM ringtone  
    			INNER JOIN ringtone_status ON ringtone.id = ringtone_status.ringtone_id 
    			INNER JOIN ringtone_extra ON ringtone.id = ringtone_extra.ringtone_id 
    			WHERE ringtone.rbt_status = 0 AND ringtone_status.rbt_approve = 2 
    			AND ringtone.imuzik_hidden_time IS NOT NULL 
    			AND length(trim(ringtone_extra.rbt_code)) > 0 
    			AND ringtone_status.rbt_delete != '1' ";
    	if(count($filters )>0){
    		if($filters['id'] != ""){
    			$sql .= " AND ringtone.id = '{$filters['id']}'";
    		}
    		if($filters['name'] != ""){
    			$sql .= "  AND ringtone.name LIKE '{$filters['name']}%'";
    		}
    		if($filters['rbtcode'] != ""){
    			$sql .= " AND ringtone_extra.rbt_code = '{$filters['rbtcode']}'";
    		}
    		 
    	}
    	$result = $con->doSelect($sql);
    	return $result[0]['total'];
    }
    
   public static function getlistRemove($limit, $offset,$filters = null)
    {
    	$con = new MysqlDAL('propel');

        /*
        $sql = "SELECT *  FROM ringtone
    			INNER JOIN ringtone_status ON ringtone.id = ringtone_status.ringtone_id 
    			INNER JOIN ringtone_extra ON ringtone.id = ringtone_extra.ringtone_id
    			WHERE ringtone.rbt_status = 0 AND ringtone_status.rbt_approve = 2 
    			AND ringtone.imuzik_hidden_time IS NOT NULL 
    			AND length(trim(ringtone_extra.rbt_code)) > 0 
    			AND ringtone_status.rbt_delete != '1' ";
         * 
         */


                $sql = "SELECT
                        ringtone.id as id,
                    ringtone.name as name,
                    ringtone.file_path_mp3 as file_path_mp3,
                    ringtone_extra.rbt_code as rbt_code,
                    ringtone.total_set as total_set,
                    ringtone.imuzik_last_downloaded as imuzik_last_downloaded,
                    ringtone.imuzik_hidden_time as imuzik_hidden_time,
                    ringtone_origin.is_upload as is_upload
                FROM ringtone
                     INNER JOIN ringtone_status ON ringtone.id = ringtone_status.ringtone_id
                     INNER JOIN ringtone_extra ON ringtone.id = ringtone_extra.ringtone_id
                     INNER JOIN ringtone_origin ON ringtone_origin.id = ringtone_extra.origin_id
                WHERE ringtone.rbt_status = 0 AND
                      ringtone_status.rbt_approve = 2 AND
                      ringtone.imuzik_hidden_time IS NOT NULL AND
                      length(trim(ringtone_extra.rbt_code)) > 0 AND
                      ringtone_status.rbt_delete != '1'";

   		 if(count($filters )>0){
    		if($filters['id'] != ""){
    			$sql .= " AND ringtone.id = '{$filters['id']}'";
    		}
    		if($filters['name'] != ""){
    			$sql .= "  AND ringtone.name LIKE '{$filters['name']}%'";
    		}
    		if($filters['rbtcode'] != ""){
    			$sql .= " AND ringtone_extra.rbt_code = '{$filters['rbtcode']}'";
    		}
    		 
    	}
    	
    	if($limit){
			$sql.= ' LIMIT '.$limit;	
		}
		if ($offset){
			$sql.=' OFFSET '.$offset;
		}
		
    	return $con->doSelect($sql);
    }
    
   public static function resum_rt_hidentime($rtid)
   {
        $currentDatetime = date('Y-m-d H:i:s', time());
       
        $con = new MysqlDAL('propel');
        $sql = "UPDATE ringtone SET
        rbt_status = 1,
        rt_status = 1,
        imuzik_hidden_time = NULL,
        imuzik_last_downloaded = '{$currentDatetime}'
        WHERE id = '".$rtid."'";
        return $con->doUpdate($sql);
   }
   
   public static function resum_rt_status($rtid)
   {
        $con = new MysqlDAL('propel');
        $currentDatetime = date('Y-m-d H:i:s');

        $sql = "UPDATE ringtone_status SET
            rbt_approve = 1,
            rbt_approve_datetime = '".$currentDatetime."',
            rbt_approve_admin = 'ims_sync',
            rbt_approve_description = 'Khoi phuc lai nhac cho tu trang thai bi xoa',
            rbt_delete = 0,
            rbt_delete_datetime = '".$currentDatetime."',
            rbt_delete_admin = 'ims_sync',
            rbt_delete_description = 'Khoi phuc lai nhac cho tu trang thai bi xoa',
            rt_approve = 1,
            rt_approve_datetime = '".$currentDatetime."',
            rt_approve_admin = 'ims_sync',
            rt_approve_description = 'Khoi phuc lai nhac chuong tu trang thai bi xoa',
            rt_delete = 0,
            rt_delete_datetime = '".$currentDatetime."',
            rt_delete_admin = 'ims_sync',
            rt_delete_description = 'Khoi phuc lai nhac chuong tu trang thai bi xoa'
            WHERE ringtone_id = '".$rtid."'";
        return $con->doUpdate($sql);
   }
   
   public static function reject_rbt_rt($id)
   {
        $con = new MysqlDAL('propel');
        $currentDatetime = date('Y-m-d H:i:s');

        $sql = "UPDATE ringtone SET
        rbt_status = 0,
        rt_status = 0,
        imuzik_hidden_time = '".$currentDatetime."'
        WHERE id = '".$id."'";
        return $con->doUpdate($sql);
   }
   
   public static function reject_rbt_rt_status($id)
   {
        $con = new MysqlDAL('propel');
        $currentDatetime = date('Y-m-d H:i:s');
        $sql = "UPDATE ringtone_status SET
        rt_approve = 2,
        rt_approve_datetime = '".$currentDatetime."',
        rt_approve_admin = 'ims_sync',
        rt_approve_description = 'Nhac chuong bi tu choi vi khong phat sinh them luot tai trong thoi gian quy dinh',
        rbt_approve = 2,
        rbt_approve_datetime = '".$currentDatetime."',
        rbt_approve_admin = 'ims_sync',
        rbt_approve_description = 'Nhac cho bi tu choi vi khong phat sinh them luot tai trong thoi gian quy dinh'
        WHERE ringtone_id = '".$id."'";
        return $con->doUpdate($sql);
   }
   
   public static function delete_rbt_rt_status($id)
   {
        $con = new MysqlDAL('propel');
        $currentDatetime = date('Y-m-d H:i:s');
        $sql = "UPDATE ringtone_status SET 
        rt_delete = 1,
        rt_delete_datetime = '".$currentDatetime."',
        rt_delete_admin = 'ims_sync',
        rt_delete_description = 'Nhac chuong bi xoa vi khong phat sinh them luot tai trong thoi gian quy dinh',
        rbt_delete = 1,
        rbt_delete_datetime = '".$currentDatetime."',
        rbt_delete_admin = 'ims_sync',
        rbt_delete_description = 'Nhac cho bi xoa vi khong phat sinh them luot tai trong thoi gian quy dinh'
        WHERE ringtone_id = '".$id."'";
        return $con->doUpdate($sql);
   }
    
    // Nhac cho da xoa
   public static function getTotalDeleted($filters = null)
   {
        $con = new MysqlDAL('propel');
        $sql = "SELECT COUNT(*) AS total FROM ringtone
                INNER JOIN ringtone_status ON ringtone.id = ringtone_status.ringtone_id
                INNER JOIN ringtone_extra ON ringtone.id = ringtone_extra.ringtone_id
                WHERE ringtone.rbt_status = 0
                AND ringtone.imuzik_hidden_time IS NOT NULL
                AND ringtone_status.rbt_delete = 1
                AND length(trim(ringtone_extra.rbt_code)) > 0";
        if(count($filters )>0){
            if($filters['id'] != ""){
                    $sql .= " AND ringtone.id = '{$filters['id']}'";
            }
            if($filters['name'] != ""){
                    $sql .= "  AND ringtone.name LIKE '{$filters['name']}%'";
            }
            if($filters['rbtcode'] != ""){
                    $sql .= " AND ringtone_extra.rbt_code = '{$filters['rbtcode']}'";
            }
    	}
    	
    	$result = $con->doSelect($sql);
    	return $result[0]['total'];
   } 
   
   public static function getlistDeleted($limit, $offset, $filters = null)
   {
        $con = new MysqlDAL('propel');
    	$sql = "SELECT *  FROM ringtone  
    			INNER JOIN ringtone_status ON ringtone.id = ringtone_status.ringtone_id 
    			INNER JOIN ringtone_extra ON ringtone.id = ringtone_extra.ringtone_id 
    			WHERE ringtone.rbt_status = 0 
    			AND ringtone.imuzik_hidden_time IS NOT NULL 
    			AND ringtone_status.rbt_delete = 1
                        AND length(trim(ringtone_extra.rbt_code)) > 0";
    	if(count($filters )>0){
            if($filters['id'] != ""){
                    $sql .= " AND ringtone.id = '{$filters['id']}'";
            }
            if($filters['name'] != ""){
                    $sql .= "  AND ringtone.name LIKE '{$filters['name']}%'";
            }
            if($filters['rbtcode'] != ""){
                    $sql .= " AND ringtone_extra.rbt_code = '{$filters['rbtcode']}'";
            }
    	}
    	
    	if($limit){
            $sql.= ' LIMIT '.$limit;
        }
        if ($offset){
            $sql.=' OFFSET '.$offset;
        }
    	return $con->doSelect($sql);
   }
   

}
